﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BookClubPortal.Models;
using BookClubPortal.Filters;
using WebMatrix.WebData;
using System.Data;

namespace BookClubPortal.Controllers
{
    [HandleError]
    [AuthorizeByDB]
    public class ManageOwnController : Controller
    {

        BookClubPortalEntities storeDB = new BookClubPortalEntities();

        //
        // GET: /ManageOwn/
         [AuthorizeByDB]
        public ActionResult Index()
        {
            var books = storeDB.Books.Include("Genre").Where(b => b.UserId == WebSecurity.CurrentUserId);
            return View(books.ToList());
            
        }

         //
         // GET: /ManageOwn/Details/5

         public ViewResult Details(int id)
         {
             Book book = storeDB.Books.Find(id);
             book.Genre = storeDB.Genres.Find(book.GenreId);
             return View(book);
         }


         //
         // GET: /ManageOwn/Create
        [AuthorizeByDB]
         public ActionResult Create()
         {
             ViewBag.GenreId = new SelectList(storeDB.Genres.OrderBy(a => a.Name), "GenreId", "Name");
             return View();
         }

        //
        // POST: /ManageOwn/Create

        [HttpPost]
        public ActionResult Create(Book book)
        {
            if (ModelState.IsValid)
            {
                book.UserId = WebSecurity.CurrentUserId;
                if (book.ImageURL == null) 
                {
                    book.ImageURL="/Content/Images/logo.png";
                }
                storeDB.Books.Add(book);
                storeDB.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.GenreId = new SelectList(storeDB.Genres, "GenreId", "Name", book.GenreId);

            return View(book);
        }


        //
        // GET: /ManageOwn/Edit/5

        public ActionResult Edit(int id)
        {
            Book book = storeDB.Books.Find(id);
            if (book.ImageURL == "/Content/Images/logo.png")
            { book.ImageURL = null; }
            ViewBag.GenreId = new SelectList(storeDB.Genres.OrderBy(a => a.Name), "GenreId", "Name", book.GenreId);
            return View(book);
        }

        //
        // POST: /ManageOwn/Edit/5

        [HttpPost]
        public ActionResult Edit(Book book)
        {
            if (ModelState.IsValid)
            {
                book.UserId = WebSecurity.CurrentUserId;
                if (book.ImageURL == null)
                {
                    book.ImageURL = "/Content/Images/logo.png";
                }
                storeDB.Entry(book).State = EntityState.Modified;
                storeDB.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.GenreId = new SelectList(storeDB.Genres, "GenreId", "Name", book.GenreId);
            return View(book);
        }

        //
        // GET: /ManageOwn/Delete/5

        public ActionResult Delete(int id)
        {
            Book book = storeDB.Books.Find(id);
            book.Genre = storeDB.Genres.Find(book.GenreId);
            return View(book);
        }

        //
        // POST: /ManageOwn/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Book book = storeDB.Books.Find(id);
            storeDB.Books.Remove(book);
            storeDB.SaveChanges();
            return RedirectToAction("Index");
        }

    }
}
